import GameConst from "../GameConst";
import SoundsManager from "../soundmanager";
import Utils from "../utils";
import AnimatedNode from "../amn/AnimatedNode";
import BitmapText from "../BitmapText";
import ClickableObject from "../ClickableObject";
import ButtonObject from "../ButtonObject";
import PopupMenu from "./PopupMenu";

export default class WinMenu extends PopupMenu {

    constructor() {
        super('win_panel');
        var t = this;
        this.stars = [];
        this.starAmount = 3;
        this.starsShowedAmount = 0;
        // this.shines = [];
        // this.starData = [
        //     [234, 199, 6.8, .82],
        //     [350, 208, 0, 1],
        //     [492, 204, -8, 1.25]
        // ];
        this.starShowLeft = 0;
      
        for(var i=0;i<this.starAmount;i++){
            var star = this.ui.getChildByName('star'+i);
            this.stars.push(star);
        }
        // var r = Utils.createBitmap("menu/mengban.png");
        // var i = r.getBounds();
        // r.width = 100 + GameConst.SCREEN_W*2;
        // r.height = 100 + GameConst.FULL_SCREEN_H*2;
        // r.y = -50;
        // r.x = -50;
        // this.grayBack = r;
        // n.addChild(r);
        // var s = Utils.createBitmap("menu/panel.png");
        // s.x = s.y = 0;
        // s.sizeGrid='0,17,0,17,1';
        // s.width = GameConst.SCREEN_W;
        // n.addChild(s);
        // var r = Utils.createBitmap("wintitle");
        // r.pivotX = r.width / 2;
        // r.pivotY = r.height / 2;
        // r.x = 347;
        // r.y = 18;
        // n.addChild(r);
        // var r = Utils.createBitmap("star_back");
        // r.x = 159;
        // r.y = 116;
        // n.addChild(r);
        // for (var o = 0; o < this.starData.length; ++o) {
        //     r = Utils.createBitmap("star_glow");
        //     var u = this.starData[o];
        //     r.x = u[0];
        //     r.y = u[1];
        //     r.scaleX = r.scaleY = 1.4 * u[3] / .82;
        //     var i = r.getBounds();
        //     r.pivotX = i.width / 2;
        //     r.pivotY = i.height / 2;
        //     n.addChild(r);
        //     this.shines.push(r);
        // }
        // for (var o = 0; o < this.starData.length; ++o) {
        //     r = Utils.createBitmap("star");
        //     var u = this.starData[o];
        //     r.x = u[0];
        //     r.y = u[1];
        //     r.rotation = u[2];
        //     r.scaleX = r.scaleY = u[3];
        //     r.pivotX = r.width / 2;
        //     r.pivotY = r.height / 2;
        //     n.addChild(r);
        //     this.stars.push(r);
        // }
        // n.addChild(this.char);
        // this.button = new AnimatedNode(GameConst.animationManager.getAnimation("button"), 1 / 30, null);
        // n.addChild(this.button);
        // this.button.addAction(97, 0, 0);
        // this.button.addAction(106, 0, -1);
        // this.button.x = 470;
        // this.button.y = 370;
        // this.button.scaleX = this.button.scaleY = .75;
        // this.button.addAction(this.button.totalFrames - 1, 1, 0);
        // var a = new ClickableObject(this.button);
        // a.setCircle(120 * this.button.scaleX, 120 * this.button.scaleX, 120 * this.button.scaleX);
        // a.callback = function () {
        //     t.onPlayDown();
        // };
        // this.clickables.push(a);
        // var f = new ButtonObject(5, function (e) {
        //     t.restarAndUnlock(e);
        // }, n, GameConst.SCREEN_W / 2, 465);
        // this.animatedButtons.push(f);
        // this.clickables.push(f);
        // this.clickables.push(new LogoObject(n, 95, 480, 1));
    }

    show () {
        super.show();
        
        this.starsShowedAmount = 0;
        this.grayBack.alpha = 0;
        Laya.Tween.to(this.grayBack,{
            alpha: 1
        }, 300, Laya.Ease.none);
        for (var n = 0; n < this.stars.length; ++n) {
            var r = this.stars[n];
            r.visible = false;
            Laya.Tween.clearAll(r);
        }
        this.starShowLeft = this.starAmount;
        var scales = [0.9,1,0.9];
        for (var n = 0; n < this.starAmount; ++n) {
            r = this.stars[n];
            r.visible = true;
            r.alpha = 0;
            r.scaleX = r.scaleY = 2;
            var s = 500 + n * 500;
            var scale = scales[n];
            Laya.Tween.to(r, {
                alpha: 1,
                scaleX: scale,
                scaleY: scale
            }, 300, Laya.Ease.none,null,s/2);
            if (r.visible) {
                SoundsManager.instance.playSound("star" + (n + 1), s)
            }
        }
        Utils.removeClip(this.scoreText);
        this.scoreText = new BitmapText("", "menu/z_");
        this.scoreText.pivotX = 0;
        this.scoreText.x = 390;
        this.scoreText.y = 200;
        this.scoreText.text = GameConst.level.score.toString();
        this.ui.addChild(this.scoreText);
        this.scoreText.letterSpacing = 1;
        this.setCharIn = .1;

        SoundsManager.instance.pauseMusic();
        SoundsManager.instance.playSound("win")
    };

    hide() {
        super.hide();
        SoundsManager.instance.resumeMusic()
    };


    onPlayDown () {
        try {
            GameConst.levelManager.loadNextLevel();
            SoundsManager.instance.playSound("pause");
        } catch (e) {
            alert("play down2 " + e);
        }
    };
  
    
    setStarData (star) {
        this.starAmount = star
    };
}






